#######################################################################
# Generate the header file with the offsets of various fields in the
# structures. These offsets can be necessary for the functions written
# in assembly. It would be much easier if we could just use 'offsetof'
# directly but this seems to be impossible.
#
# Based on the top-level KBuild file of the Linux kernel, as of 
# version 3.3.0.

offsets-file := kedr_asm_offsets.h

always  += $(offsets-file)
targets += $(offsets-file)
targets += kedr_asm_offsets.s

# Default sed regexp - multiline due to syntax constraints
define sed-y
	"/^->/{s:->#\(.*\):/* \1 */:; \
	s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
	s:->::; p;}"
endef

quiet_cmd_kedr_offsets = GEN     $@
define cmd_kedr_offsets
	(set -e; \
	 echo "#ifndef KEDR_ASM_OFFSETS_H_1258_INCLUDED"; \
	 echo "#define KEDR_ASM_OFFSETS_H_1258_INCLUDED"; \
	 echo "/*"; \
	 echo " * DO NOT MODIFY."; \
	 echo " *"; \
	 echo " * This file was generated by Kbuild"; \
	 echo " *"; \
	 echo " */"; \
	 echo ""; \
	 sed -ne $(sed-y) $<; \
	 echo ""; \
	 echo "#endif" ) > $@
endef

ccflags-y += -I$(src)

# We use internal kbuild rules to avoid the "is up to date" message from make
$(obj)/kedr_asm_offsets.s: kedr_asm_offsets.c FORCE
	$(call if_changed_dep,cc_s_c)

$(obj)/$(offsets-file): $(obj)/kedr_asm_offsets.s Kbuild
	$(call cmd,kedr_offsets)
#######################################################################
